package com.atguigu.tingshu.user.config;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@Component
public class WeChatMpConfig {

    @Autowired
    private WechatAccountConfig wechatAccountConfig;

    /**
     *  微信小程序服务,标记当前类为 Spring 组件，Spring 会自动扫描并将其实例化到容器中，作为配置类管理微信小程序服务的 Bean
     * @return
     */
    @Bean
    public WxMaService wxMaService(){
        //  创建配置对象,创建微信小程序配置的默认实现类实例。
        WxMaDefaultConfigImpl wxMaDefaultConfig = new WxMaDefaultConfigImpl();
        wxMaDefaultConfig.setAppid(wechatAccountConfig.getAppId());
        wxMaDefaultConfig.setSecret(wechatAccountConfig.getAppSecret());
        //  返回对象
        WxMaServiceImpl wxMaService = new WxMaServiceImpl();
        wxMaService.setWxMaConfig(wxMaDefaultConfig);
        return  wxMaService;
    }
}